package com.liu.controller;


import com.github.pagehelper.PageInfo;
import com.liu.model.CommunityPost;
import com.liu.service.CommunityPostService;
import com.liu.tools.ActionContextUtils;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by liu on 2018/3/26.
 */
@RestController
    @RequestMapping(value = "/community-post")
public class CommunityPostController {

    @Autowired
    private CommunityPostService communityPostService;

    @ResponseBody
    @PostMapping(value = "/add", produces = {"application/json;charset=UTF-8"})
    public String add(HttpServletRequest req, CommunityPost communityPost) {
        communityPost.setCreateTime(new Date());
        communityPostService.add(communityPost);
        return "success";
    }

    @ResponseBody
    @PostMapping(value = "/edit", produces = {"application/json;charset=UTF-8"})
    public String edit(HttpServletRequest req, CommunityPost communityPost) {
        communityPostService.edit(communityPost);
        return "success";
    }

    @RequestMapping(value = "/list", produces = {"application/json;charset=UTF-8"})
    public PageInfo findAllCommunityPost(int pageNum, int pageSize) {
        Map<String, Object> queryMap = new HashMap<String, Object>(0);
        String communityInfoId = ActionContextUtils.getParameter("communityInfoId");
        if(communityInfoId != null && !communityInfoId.equals("")) {
            queryMap.put("communityInfoId", communityInfoId);
        }
        List<CommunityPost> list = communityPostService.findAll(pageNum, pageSize, queryMap);
        return new PageInfo(list);
    }

    @RequestMapping(value = "/detail", produces = {"application/json;charset=UTF-8"})
    public CommunityPost findCommunityPost(String id) {
        CommunityPost communityPost = communityPostService.findById(id);
        return communityPost;
    }

    @ResponseBody
    @PostMapping(value = "/delete", produces = {"application/json;charset=UTF-8"})
    public String del(String id) {
        communityPostService.deleteById(id);
        return "success";
    }
}